package com.mfp.java.testYou.ui.mvc;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.mfp.java.testYou.core.AppContext;
import com.mfp.java.testYou.core.servlets.TakeTestServlet;
import com.mfp.java.testYou.db.model.Answer;
import com.mfp.java.testYou.db.model.AnsweredQuestion;
import com.mfp.java.testYou.db.model.Question;
import com.mfp.java.testYou.model.UiSelectPair;

public class AnswerQuestionPage extends AbstractMVC {

	private static final Logger log = Logger.getLogger(AnswerQuestionPage.class);

	public static final String URL = "/testYou/pages/answerQuestion.jsp";

	public static final String ATTR_CURR_QUESTION = "testUquestion";
	public static final String ATTR_QUESTION_NO = "testUquestionNo";
	public static final String ATTR_ALLOW_HINTS = "testUtestAllowHints";

	private Question question;
	private Integer qNo;
	private boolean allowHints;

	public AnswerQuestionPage(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		init(request);
	}

	private void init(HttpServletRequest req) {
		Object o = req.getAttribute(ATTR_CURR_QUESTION);

		if (o instanceof Question) {
			question = (Question) o;
		} else {
			log.error("No question found");
		}

		Object o2 = req.getAttribute(ATTR_QUESTION_NO);

		if (o2 instanceof Integer) {
			qNo = (Integer) o2;
		} else {
			log.error("No question number found");
			qNo = 0;
		}
		if (qNo >= AppContext.getUserSession(getRequest()).getTakenTest().getQuestionIds().size()) {
			log.debug("Value is larger then maximum, setting 0");
			qNo = 0;
		}
		
		Object o3 = req.getAttribute(ATTR_ALLOW_HINTS);
		if (o3 instanceof Boolean){
			allowHints = (Boolean) o3; 
		} else {
			log.error("No allow hints found. Setting false");
			allowHints = false;
		}
	}

	public String getQuestionText() {
		return (question == null) ? "" : question.getText();
	}

	public String getQuestionHint() {
		return (question == null) ? "" : question.getHint();
	}

	public List<UiSelectPair> getQuestionAnswers() {
		if (question == null) {
			return new LinkedList<UiSelectPair>();
		}
		List<UiSelectPair> list = new LinkedList<UiSelectPair>();
		for (Answer a : question.getAnswers()) {
			list.add(new UiSelectPair(a.getText(), a.getId()));
		}
		return list;
	}

	public Integer getCurrentQuestionNumber() {
		return qNo;
	}

	public Integer getNumberOfQuestions() {
		return AppContext.getUserSession(getRequest()).getTakenTest().getAnswers().size();
	}

	public List<String> getQuestionClasses() {
		List<String> list = new LinkedList<String>();
		for (int i = 0; i < getNumberOfQuestions(); i++) {
			list.add("class1");
		}
		return list;
	}

	public List<String> getCorrectIds() {
		List<String> ids = new LinkedList<String>();

		AnsweredQuestion aq = AppContext.getUserSession(getRequest()).getTakenTest().getAnswers().get(qNo);
		Boolean[] givenA = aq.getGivenAnswers();

		if (givenA != null) {
			for (int i = 0; i < question.getAnswers().size(); i++) {
				if (givenA[i]) {
					ids.add(TakeTestServlet.INPUT_ANSWER + question.getAnswers().get(i).getId());
				}
			}
		}

		return ids;
	}
	
	public boolean isHintsAllowed(){
		return allowHints;
	}
}
